import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_common_app/provider_model/audio_model.dart';
import 'package:flutter_common_app/utils/common_util/date_util.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';


class ProgressSlider extends StatelessWidget {
  final AudioModel model;

  const ProgressSlider(this.model,{Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<String>(
      stream: model.curPositionStream,
      builder: (context, AsyncSnapshot<dynamic> snapshot) {
        if (snapshot.hasData) {
          var totalTime =
          snapshot.data.substring(snapshot.data.indexOf('-') + 1);
          var curTime = double.parse(snapshot.data.substring(0, snapshot.data.indexOf('-')));
          var curTimeStr = DateUtil.formatDateMs(curTime.toInt(), format: "mm:ss");
          return buildProgress(curTime, totalTime, curTimeStr);
        } else {
          return buildProgress(0, "0", "00:00");
        }
      },
    );
  }

  Widget buildProgress(
      double curTime, String totalTime, String curTimeStr) {
    return Container(
      width: 750.w,
      padding: EdgeInsets.symmetric(horizontal: 30.w),
      child: Row(
        children: [
          Text(
            curTimeStr,
            style: TextStyle(
              fontSize: 20.sp,
              color: Colors.white,
              decoration: TextDecoration.none,
            ),
          ),
          Expanded(
            child: SliderTheme(
              data: SliderThemeData(
                trackHeight: 2.w,
                thumbShape: RoundSliderThumbShape(
                  enabledThumbRadius: 10.w,
                ),
              ),
              child: Slider(
                value: curTime,
                onChanged: (data) {
                  model.sinkProgress(data.toInt());
                },
                onChangeStart: (data) {
                  model.pausePlay();
                },
                onChangeEnd: (data) {
                  model.seekPlay(data.toInt());
                },
                activeColor: Colors.white,
                inactiveColor: Colors.white30,
                min: 0,
                max: double.parse(totalTime),
              ),
            ),
          ),
          Text(
            DateUtil.formatDateMs(int.parse(totalTime), format: "mm:ss"),
            style: TextStyle(
              fontSize: 20.sp,
              color: Colors.white,
              decoration: TextDecoration.none,
            ),
          ),
        ],
      ),
    );
  }
}
